package com.sum.tea.xinlian

import android.app.Dialog
import android.os.Bundle
import android.view.KeyEvent
import android.widget.TextView
import android.widget.Toast
import com.blankj.utilcode.util.ToastUtils
import com.sum.common.base.BaseMvvmActivity
import com.sum.tea.R
import com.sum.tea.databinding.ActivityUpdateBinding
import com.sum.tea.xinlian.dialog.OpenFileDialog
import com.sum.tea.xinlian.viewmodel.UpdateViewModel

class UpdateActivity : BaseMvvmActivity<ActivityUpdateBinding, UpdateViewModel>() {
    private lateinit var dialog: Dialog
    private val openfileDialogId = 0

    override fun initView(savedInstanceState: Bundle?) {
        setupUI()
        observeViewModel()
    }

    private fun setupUI() {
        mBinding.buttonSelectfile.setOnClickListener {
            mViewModel.setFileExtension("mdfw")
            showDialog(openfileDialogId)
        }

        mBinding.buttonSelectfile2.setOnClickListener {
            mViewModel.setFileExtension("bin")
            showDialog(openfileDialogId)
        }

        mBinding.buttonStartupdate.setOnClickListener {
            val filename = findViewById<TextView>(R.id.textView_path).text.toString()
            if (filename.isEmpty() || filename.endsWith("path")) {
                ToastUtils.showShort("选择升级文件/*.${mViewModel.fileExtension.value}")
                return@setOnClickListener
            }

            mViewModel.startUpdate(filename)
            mBinding.progressBar.progress = 0
            ToastUtils.showShort("升级开始")
        }
    }

    private fun observeViewModel() {
        mViewModel.progress.observe(this) { progress ->
            progress?.let {
                mBinding.progressBar.progress = it.toInt()
                findViewById<TextView>(R.id.textView_ps).text = "$it%"
            }
        }

        mViewModel.statusMessage.observe(this) { message ->
            message?.let {
                findViewById<TextView>(R.id.textView_ps).text = it
                Toast.makeText(this, it, Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onCreateDialog(id: Int): Dialog? {
        return if (id == openfileDialogId) {
            val images = mapOf(
                OpenFileDialog.sRoot to R.drawable.filedialog_root,
                OpenFileDialog.sParent to R.drawable.filedialog_folder_up,
                OpenFileDialog.sFolder to R.drawable.filedialog_folder,
                mViewModel.fileExtension.value to R.drawable.filedialog_file,
                OpenFileDialog.sEmpty to R.drawable.filedialog_root
            )
            dialog = OpenFileDialog.createDialog(id, this, "打开文件", { bundle ->
                val filepath = bundle.getString("path")
                findViewById<TextView>(R.id.textView_path).text = filepath
                dialog.dismiss()
                removeDialog(openfileDialogId)
            },
                "." + mViewModel.fileExtension.value + ";",
                images
            )
            dialog.setOnKeyListener { _, keyCode, _ ->
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    removeDialog(openfileDialogId)
                }
                true
            }
            dialog
        } else {
            null
        }
    }
}


